package com.zhang.service.impl;

import com.zhang.mapper.CategoryMapper;
import com.zhang.pojo.Category;
import com.zhang.service.CategoryService;
import com.zhang.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**文章分类相关接口
 * @program: big_event
 * @description: TODO
 * @author: Mr.Zhang
 * @create: 2024-03-22 13:24
 **/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     *  新增文章分类
     * @param category
     */
    @Override
    public void add(Category category) {
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);
        categoryMapper.add(category);
    }

    /**
     * 查询所有文章分类
     * @return
     */
    @Override
    public List<Category> list() {
        Map<String,Object>  map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        categoryMapper.list(userId);
        return categoryMapper.list(userId);
    }

    /**
     * 根据ID查询分类信息
     * @param id
     * @return
     */
    @Override
    public Category findByid(Integer id) {
        Category c =  categoryMapper.findByid(id);
        return c;
    }

    /**
     * 更新分类
     * @param category 分类对象
     */
    @Override
    public void update(Category category) {
        // 设置更新时间为当前时间
        category.setUpdateTime(LocalDateTime.now());
        // 调用分类Mapper的update方法更新分类信息
        categoryMapper.update(category);
    }


    /**
     * 根据ID删除文章分类
     * @param id
     */
    @Override
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }
}
